package cn.gov.cus.rmftquartz.config;

import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.icatch.jta.UserTransactionManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.interceptor.TransactionInterceptor;
import org.springframework.transaction.jta.JtaTransactionManager;

import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
import java.util.Properties;

/**
 * 多数据源用同一个事务管理，其他配置管理数据源的类都调这的事务。用于需要在一个service方法中回滚多个不同数据源的需求。（实际不同数据源用不同的各自的事务的话，把不同数据源的方法写到不同service方法里也可以通过单个自己的数据源（开始时的那种简单版本的配置数据源类里各自的事务管理）来操作管理。
 * @author wxming.
 * @date 2018/2/9.
 */
@Configuration
@EnableTransactionManagement
public class TransactionManagerConfig {

    @Bean(name="userTransaction")
    public UserTransaction userTransaction() throws Throwable{
        UserTransactionImp userTransactionImp = new UserTransactionImp();
        userTransactionImp.setTransactionTimeout(10000);
        return userTransactionImp;
    }

    @Bean(name="atomikosTransactionManager",initMethod = "init",destroyMethod = "close")
    public TransactionManager atomikosTransactionManager(){
        UserTransactionManager userTransactionManager = new UserTransactionManager();
        userTransactionManager.setForceShutdown(true);
        return userTransactionManager;
    }

    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager(
            @Qualifier("userTransaction") UserTransaction userTransaction,
            @Qualifier("atomikosTransactionManager") TransactionManager atomikosTransactionManager){
        return new JtaTransactionManager(userTransaction,atomikosTransactionManager);
    }

    @Bean(name = "transactionInterceptor")
    public TransactionInterceptor transactionInterceptor(PlatformTransactionManager platformTransactionManager){
        TransactionInterceptor transactionInterceptor = new TransactionInterceptor();
        //事务管理器
        transactionInterceptor.setTransactionManager(platformTransactionManager);
        Properties transactionAttributes = new Properties();

        //test
        transactionAttributes.setProperty("test*","PROPAGATION_REQUIRED,-Throwable");

        //新增
        transactionAttributes.setProperty("insert*","PROPAGATION_REQUIRED,-Throwable");
        //修改
        transactionAttributes.setProperty("update*","PROPAGATION_REQUIRED,-Throwable");
        //删除
        transactionAttributes.setProperty("delete*","PROPAGATION_REQUIRED,-Throwable");
        //查询
        transactionAttributes.setProperty("select*","PROPAGATION_REQUIRED,-Throwable,readOnly");

        transactionInterceptor.setTransactionAttributes(transactionAttributes);
        return transactionInterceptor;
    }
}
